const app = getApp();
const publics = require('../../../utils/public.js');
const md5 = require('../../../utils/md5.js');
const http = require('../../../utils/http.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        personMsg: [
            { title: '头像', img: '1', imgone: '', msg: '', select: 0, url: '' },
            { title: '昵称', img: '', imgone: '/images/sq_ico2.png', msg: '不啦不啦', select: 1, url: '' },
            { title: '姓名', img: '', imgone: '/images/sq_ico2.png', msg: '未设置', select: 2, url: '' },
            { title: '手机号', img: '', imgone: '/images/sq_ico2.png', msg: '188****8888', select: 3, url: '' },
            { title: '实名认证', img: '', imgone: '', msg: '已认证', select: 4, url: '' }


        ],
        select: 0,
        isOpen: 'getUserInfo',
        tempFilePaths: '/images/wd2.png'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      
    },
    clickcte(e) {
       // console.log(e.currentTarget.dataset.select)
        switch (e.currentTarget.dataset.select) {
            case 0:
                var that = this;
                wx.chooseImage({
                    count: 1, // 默认9  
                    sizeType: ['original', 'compressed'],  
                    sourceType: ['album', 'camera'], 
                    success: function (res) {
                        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
                        console.log(res.tempFilePaths)
                        var tempFilePaths = res.tempFilePaths[0]                       
                        that.setData({
                            tempFilePaths,                            
                        })                                                                                        
                        var token = wx.getStorageSync('token')
                        var data = {
                            method: 'File.uploadImg',
                            token: token,
                            type: 5,
                        }
                        var arr = http.sorts(data)
                        data.sign = md5.hex_md5(arr)
                        wx.uploadFile({
                            url: http.http,
                            filePath: tempFilePaths,
                            name: 'file',
                            formData: data, success(res) {
                                const data = JSON.parse(res.data)
                                if (JSON.parse(res.data).code == 0) {
                                    var msg = {
                                        method: 'User.head_portrait',
                                        token: token,
                                        head_portrait: JSON.parse(res.data).data.url
                                    }
                                    var msgArr = http.sorts(msg)
                                    msg.sign = md5.hex_md5(msgArr)
                                    http._post(msg, res => {
                                        // console.log(res)
                                        that.onShow()
                                        wx.showToast({
                                            title: res.data.msg,
                                            icon: 'none',
                                            duration: 2000
                                        })
                                    })
                                } else {
                                    wx.showToast({
                                        title: '失败',
                                        icon: 'none',
                                        duration: 2000
                                    })
                                }
                            }
                        })



                    }
                })
                break;
            case 1:
                wx.navigateTo({
                    url: '/pages/myinfo/personmsg/myname/myname?title=修改昵称&placeText=快来取个有逼格的签名吧！&id=0',
                })
                break;
            case 2:
                // wx.navigateTo({
                //   url: '/pages/myinfo/personmsg/myname/myname?title=修改姓名&placeText=请输入姓名&id=1',
                // })
                break;
            case 3:
                wx.navigateTo({
                    url: '/pages/myinfo/personmsg/phone/phone',
                })
                break;
            case 4:
                break;
            case 5:
                break;
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */

    onReady: function () {

    },
    bindGetUserInfo(e) {
        var _this = this;
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                    _this.setData({
                        select: 5,
                        isOpen: 'openSetting'
                    })
                } else {
                    console.log(1)
                    _this.setData({
                        isOpen: 'getUserInfo',
                        select: 0
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (e) {
        var token = wx.getStorageSync('token')
        var data = {
            method: 'User.getInfo',
            token: token,
        }
        var arr = http.sorts(data)
        var that = this
        data.sign = md5.hex_md5(arr)
        http._post(data, res => {
            console.log(res)
            console.log(res.data.data.userInfo)
            var userInfo = res.data.data.userInfo;
            var personMsg = that.data.personMsg;
            var tempFilePaths = that.data.tempFilePaths
            tempFilePaths = userInfo.head_portrait
            personMsg[1].msg = userInfo.nickname
            personMsg[2].msg = userInfo.name
            personMsg[3].msg = userInfo.telephone
            userInfo.is_auth == 0 ? personMsg[4].msg = "未认证" : personMsg[4].msg = "已认证"
            that.setData({
                personMsg,
                tempFilePaths
            })
            if (userInfo.is_auth = 0) {

            }
        })   
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onLaunch: function () {

    },


    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})